package pythonweed

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

func GenerateSetUp(params interface{}) (*Result, error) {
	mJson, err := json.Marshal(params)
	fmt.Println(err)
	contentReader := bytes.NewReader(mJson)

	req, _ := http.NewRequest("POST", "http://127.0.0.1:4040/setup1docx", contentReader)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	s, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		return nil, err
	}
	result := &Result{}
	if err = json.Unmarshal(s, result); err != nil {
		return nil, err
	}
	return result, nil
}

type Result struct {
	Status int    `json:"status"`
	Size   int    `json:"size"`
	Fid    string `json:"fid"`
	Msg    string `json:"msg"`
}

func GenerateSetUp2(params interface{}) (*Result, error) {
	mJson, err := json.Marshal(params)
	fmt.Println(err)
	contentReader := bytes.NewReader(mJson)

	req, _ := http.NewRequest("POST", "http://127.0.0.1:4040/setup2docx", contentReader)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	s, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		return nil, err
	}
	result := &Result{}
	if err = json.Unmarshal(s, result); err != nil {
		return nil, err
	}
	return result, nil
}

func GenerateSetUp3(params interface{}) (*Result, error) {
	mJson, err := json.Marshal(params)
	fmt.Println(err)
	contentReader := bytes.NewReader(mJson)

	req, _ := http.NewRequest("POST", "http://127.0.0.1:4040/setup3docx", contentReader)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	s, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		return nil, err
	}
	result := &Result{}
	if err = json.Unmarshal(s, result); err != nil {
		return nil, err
	}
	return result, nil
}

func GenerateMediumTerm(params interface{}) (*Result, error) {
	mJson, err := json.Marshal(params)
	fmt.Println(err)
	fmt.Println(string(mJson))
	contentReader := bytes.NewReader(mJson)

	req, _ := http.NewRequest("POST", "http://127.0.0.1:4040/setup4docx", contentReader)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	s, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		return nil, err
	}
	result := &Result{}
	if err = json.Unmarshal(s, result); err != nil {
		return nil, err
	}
	return result, nil
}

func GenerateFinsihReport(params interface{}) (*Result, error) {
	mJson, err := json.Marshal(params)
	fmt.Println(err)
	contentReader := bytes.NewReader(mJson)

	req, _ := http.NewRequest("POST", "http://127.0.0.1:4040/setup5docx", contentReader)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	s, err := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	if err != nil {
		return nil, err
	}
	result := &Result{}
	if err = json.Unmarshal(s, result); err != nil {
		return nil, err
	}
	return result, nil
}
